package view.plugin
{
	//全局消息框
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import flash3.framework.Singleton;
	
	import global.Define;
	
	public class MessageBox extends Sprite
	{
		private var main:MovieClip = null;
		private var callOK:Function = null;
		private var callCansel:Function = null;
		public static const BTN_OK:uint = 1;
		public static const BTN_CANSEL:uint = 2;
		public function MessageBox()
		{
			super();
			
			main = new MessageBoxPanel();
			addChild(main);
			
			main.btnOK.addEventListener(MouseEvent.CLICK, onClickBtnOK);
			main.btnCansel.addEventListener(MouseEvent.CLICK, onClickBtnCansel);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		
		private function onAddToStage(evt:Event):void
		{
			this.x = (Define.SCREEN_WIDTH - main.width) / 2;
			this.y = (Define.SCREEN_HEIGHT - main.height) / 2;
		}
		
		public function show(type:uint, message:String, okCall:Function=null, canselCall:Function=null):void
		{
			callOK = okCall; callCansel = canselCall;
			main.txtMessage.text = message;
			
			var showBtnOK:Boolean = main.btnOK.visible = Boolean(type & BTN_OK);
			var showBtnCansel:Boolean = main.btnCansel.visible = Boolean(type & BTN_CANSEL);
			if(showBtnOK && showBtnCansel)
			{
				main.btnOK.x = 66;
				main.btnCansel.x = 235;
			}
			else if(showBtnOK)
			{
				main.btnOK.x = 145;
			}
			
			var sprite:Sprite = ChessLobby.dialog;
			while(sprite.numChildren > 0)
				sprite.removeChildAt(0);
			sprite.addChild(this);
			ChessLobby.masks.visible = true;
		}
		public function set text(t:String):void
		{
			main.txtMessage.text = t;
		}
		
		public function close():void
		{
			if(parent)
			{
				parent.removeChild(this);
			}
			ChessLobby.masks.visible = false;
		}
		
		private function onClickBtnOK(evt:MouseEvent):void
		{
			if(callOK != null) 
				callOK();
			close();
		}
		
		private function onClickBtnCansel(evt:MouseEvent):void
		{
			if(callCansel != null) 
				callCansel();
			close();
		}
		
		static public function get i():MessageBox
		{
			return Singleton.getInstanceOrCreate(MessageBox) as MessageBox;
		}
	}
}